import { resolve } from 'node:path'
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'

const LIB_NAME = 'vue3-component-library'

export default () => {
  // https://vitejs.dev/config/
  return defineConfig({
    // If our .vue files have a style, it will be compiled as a single `.css` file under /dist.
    plugins: [Vue()],
  
    build: {
      // Output compiled files to /dist.
      outDir: './dist',
      lib: {
        // Set the entry point (file that contains our components exported).
        entry: resolve(__dirname, 'src/index.ts'),
        // Name of the library.
        name: LIB_NAME,
        // We are building for CJS and ESM, use a function to rename automatically files.
        // Example: my-component-library.esm.js
        fileName: (format) => `${LIB_NAME}.${format}.js`,
      },
      rollupOptions: {
        // Vue is provided by the parent project, don't compile Vue source-code inside our library.
        external: ['vue'],
        output: { 
          // disable warning on src/index.ts using both default and named export
          exports: 'named',
          globals: { 
            vue: 'Vue' 
          } 
        },
      },
      emptyOutDir: false, // to retain the types folder generated by tsc
    }
  })
}
